﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BrainTechLLC;
using BrainTechLLC.Silverlight.Controls;

namespace BrainTechLLC.Silverlight.ClientApplication
{
    public partial class EditBasicTemplate : BaseDataVisualizer, ISingleItemDataVisualizer
    {
        public override void SetVisualizedData(DataVisualizer vis, object o) { TemplateObject = o; base.SetVisualizedData(vis, o); }
        public Type VisualizeType { get { return typeof(Template); } }
        public string VisualizerName { get { return "EditBasicTemplate"; } }

        public static readonly DependencyProperty TemplateObjectProperty = DependencyProperty.Register(
            "TemplateObject", typeof(object), typeof(EditBasicTemplate), new PropertyMetadata(null, new PropertyChangedCallback(TemplateObjectChanged)));

        public static void HandleChange<T2>(DependencyObject d, DependencyPropertyChangedEventArgs e, Action<EditBasicTemplate, T2> action)
        {
            d.HandleChange<EditBasicTemplate, T2>(e, action, (t) => t.UpdateBindings());
        }

        public object TemplateObject
        {
            get { return (object)GetValue(TemplateObjectProperty); }
            set { SetValue(TemplateObjectProperty, value); }
        }

        private static void TemplateObjectChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandleChange<object>(d, e, (t, val) => t.TemplateObject = val);
        }

        public override void UpdateBindings()
        {
            if (gridData != null)
                gridData.DataContext = TemplateObject;
        }

        public override string TitleText
        {
            get
            {
                Template t = TemplateObject as Template;

                return (t == null) ? string.Empty :
                    string.Format("Template {0} ({1})", t.FriendlyName, t.PropertiesCount);
            }
        }

        public EditBasicTemplate()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(EditBasicTemplate_Loaded);
        }

        void EditBasicTemplate_Loaded(object sender, RoutedEventArgs e)
        {
        }
    }
}